package ph.com.gs3.formalistics.model.ldao;

import ph.com.gs3.formalistics.model.LocalDataAccessObject;
import ph.com.gs3.formalistics.model.tables.CompaniesTable;
import ph.com.gs3.formalistics.model.vo.business.Company;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class CompaniesLDAO extends LocalDataAccessObject {

	public CompaniesLDAO(Context context) {
		super(context);
	}

	public Company saveCompany(Company company) throws InsertFailedException {

		ContentValues cv = createCVFromCompany(company);

		long insertId = database.insert(CompaniesTable.NAME, null, cv);

		if (insertId <= 0) {
			throw new InsertFailedException("Failed inserting company.");
		}

		return getCompany((int) insertId);

	}

	public Company getCompany(int id) {

		String whereClause = CompaniesTable.COL_ID + " = ?";
		String[] whereArgs = { Integer.toString(id) };

		Cursor cursor = database.query(CompaniesTable.NAME, CompaniesTable.COLUMN_COLLECTION,
		        whereClause, whereArgs, null, null, null);

		cursor.moveToFirst();

		Company company = null;

		if (!cursor.isAfterLast()) {
			company = cursorToCompany(cursor);
		}

		cursor.close();

		return company;

	}

	public Company getCompany(String webId, String server) {

		// @formatter:off
		String whereClause 	= CompaniesTable.COL_WEB_ID + " = ? AND " 
							+ CompaniesTable.COL_SERVER + " = ?";
		String[] whereArgs 	= { webId, server };
		// @formatter:on		
		
		Cursor cursor = database.query(CompaniesTable.NAME, CompaniesTable.COLUMN_COLLECTION,
		        whereClause, whereArgs, null, null, null);

		cursor.moveToFirst();

		Company company = null;

		if (!cursor.isAfterLast()) {
			company = cursorToCompany(cursor);
		}

		cursor.close();

		return company;
	}

	private ContentValues createCVFromCompany(Company company) {

		ContentValues cv = new ContentValues();
		
		cv.put(CompaniesTable.COL_WEB_ID, company.getWebId());
		cv.put(CompaniesTable.COL_NAME, company.getName());
		cv.put(CompaniesTable.COL_SERVER, company.getServer());

		return cv;

	}

	private Company cursorToCompany(Cursor cursor) {

		// @formatter:off
		int idIndex 	= cursor.getColumnIndexOrThrow(CompaniesTable.COL_ID);
		int webIdIndex 	= cursor.getColumnIndexOrThrow(CompaniesTable.COL_WEB_ID);
		int nameIndex 	= cursor.getColumnIndexOrThrow(CompaniesTable.COL_NAME);
		int serverIndex = cursor.getColumnIndexOrThrow(CompaniesTable.COL_SERVER);		
		
		int id 			= cursor.getInt(idIndex);
		String webId 	= cursor.getString(webIdIndex);
		String name 	= cursor.getString(nameIndex);
		String server 	= cursor.getString(serverIndex);
		// @formatter:on

		Company company = new Company();

		company.setId(id);
		company.setWebId(webId);
		company.setName(name);
		company.setServer(server);

		return company;

	}
	
	public static Company cursorToCompanyWithAliasedColumns(Cursor cursor) {

		// @formatter:off
		int idIndex 	= cursor.getColumnIndexOrThrow("company_id");
		int webIdIndex 	= cursor.getColumnIndexOrThrow("company_web_id");
		int nameIndex 	= cursor.getColumnIndexOrThrow("company_name");
		int serverIndex = cursor.getColumnIndexOrThrow("server");
		
		int id 			= cursor.getInt(idIndex);
		String webId 	= cursor.getString(webIdIndex);
		String name 	= cursor.getString(nameIndex);
		String server 	= cursor.getString(serverIndex);
		// @formatter:on

		Company company = new Company();

		company.setId(id);
		company.setWebId(webId);
		company.setName(name);
		company.setServer(server);

		return company;
	}

}
